﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform.Dc
{
    /// <summary>
    /// 药品类别
    /// </summary>
    public class DrugTypeService : CrudAppService<DrugType, DrugTypeDto, Guid, DrugTypeInput, DrugTypeCreateDto, DrugTypeUpdateDto>
    {
        private IRepository<DrugType, Guid> _drugCatalogTypeRepository;

        public DrugTypeService(IRepository<DrugType, Guid> drugCatalogTypeRepository) : base(drugCatalogTypeRepository)
        {
            _drugCatalogTypeRepository = drugCatalogTypeRepository;
        }

        /// <summary>
        /// 获取药品类别字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<DrugTypeDto>> GetListAsync(DrugTypeInput input)
        {
            var iQuery = await _drugCatalogTypeRepository.GetQueryableAsync();

            if (input.SkipCount == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DrugType>, List<DrugTypeDto>>(iQuery.ToList());

                return new PagedResultDto<DrugTypeDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.SkipCount, input.MaxResultCount);
                var itemsDto = ObjectMapper.Map<List<DrugType>, List<DrugTypeDto>>(items.ToList());

                return new PagedResultDto<DrugTypeDto>(totalCount, itemsDto);
            }
        }
    }
}
